
const CardEnum = {
    BJ: 1 << 0,
    SZ: 1 << 1,
    THU: 1 << 2,
    CU: 1 << 3,
    UnionPay1: 1 << 4,
    UnionPay2: 1 << 5,
    UnionPay3: 1 << 6,
    Wuhan: 1 << 7,
    CQ: 1 << 8,
    TU: 1 << 9,
    VISA: 1 << 10,
    MC: 1 << 11,
    ChinaID: 1 << 12,
    MC2: 1 << 13,
    Octo: 1 << 14,
    TMoney: 1 << 15,
    VISA_UP: 1 << 16,
    Macau: 1 << 17,
    ALL: 0xFFFFFF,
};
const PresetAPDU = [
    [CardEnum.BJ, '00B0840020', '10007515114099280102003000000000000000000000000020180410202404109000'],
    [CardEnum.BJ | CardEnum.Wuhan | CardEnum.CU,
        '00A4000002100100', '6F08840450424F43A5009000'],
    [CardEnum.SZ, '00A4000002100100', '6f3284075041592e535a54a5279f0801029f0c200000000000000000fd21000051800000a344c2132017031420270314101000009000'],
    // [CardEnum.ALL, '00B08400020', '6A82'],
    [CardEnum.SZ, '00B095001C', '0000000000000000fd21000051800000a344c21320170314202703149000'],
    [CardEnum.Macau, '00A4000002100100', '6F42840AB0C4C3C5CDA8C7AEB0FCA5349F0801029F0C2D000108530000000001010000417FC592F4208933202107122041071285000000000000603205133300000000009000'],
    [CardEnum.Macau, '00B201C417', '00080000000000001E09000000011735202108201754449000'],
    [CardEnum.Macau, '00B202C417', '00070000000000001E09000000011410202108112045509000'],
    [CardEnum.Macau, '00B203C417', '00060000000000002809000000011597202108102036309000'],

    [CardEnum.THU | CardEnum.CQ, '00A40000023F00', '9000'],
    [CardEnum.THU, '00B0950021', '0009000401f1000550a3000119022522073100041237007d01f407d01e001401019000'],
    [CardEnum.THU, '00B0960026', 'd0c0d1eecf4800000000000000000000000000003030303030303081323031373031303033389000'],
    [CardEnum.THU, '00A4040009A0000000038698070100', '6f328409a00000000386980701a5259f0801019f0c1e62640022333300010301000000000000100011aa201301012015123155669000'],
    [CardEnum.TMoney, '00A4040007D410000003000100',  '6F31B02F0010010810100101528215340530238841201906262024062501000007A120500000000000000000000000000000009000'],
    [CardEnum.TMoney, '904C000004', '000000009000'],
    [CardEnum.TMoney, '00B201242E', '012C00000000000000020000000007200100200010080081CFB3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9000'],
    [CardEnum.TMoney, '00B2011C34', '013200001122000000000000112233445566779000'],

    [CardEnum.CU, '00A4040009A0000000038698070100', '6F2E8409A00000000386980701A5219F0C1E0000000100000000010100010102030405060708201701012027123100009000'],
    [CardEnum.CU, '00B095001E', '0000000100000000010100010102030405060708201701012027123100009000'],

    [CardEnum.TU, '00A4040008A00000063201010500', '6F318408A000000632010105A5259F0801029F0C1E01011000FFFFFFFF020103105170070104791381201907182040123100009000'],
    [CardEnum.TU, '00B097000B', '00000156580058400001019000'],

    [CardEnum.ChinaID, '0036000008', '1122334455667788900000'],
    [CardEnum.ChinaID, '00A40000026002', '900000'],
    [CardEnum.ChinaID, '80B0000020', '0001302a0290090500000000000234d56be9911ee0a0443795a0056181833f11900000'],

    [CardEnum.CQ, '00A4040009A0000000038698070100', '6F348409A00000000386980701A5279F0801999F0C2000014000201412032099123100000000000000000000000000000000000000009000'],
    [CardEnum.CQ, '00B095001E', '0001400020141203209912310000000000000000000000000000000000009000'],
    [CardEnum.CQ, '00B0850030', '847540000000FFFF010040004000000212668612201405120002C0A80A181920141203000000000000000000000000009000'],
    [CardEnum.THU, '805C000104', '8000164e9000'],
    [CardEnum.ALL ^ CardEnum.THU, '805C000204', '8000164e9000'],

    [CardEnum.ALL, '00B201C417', '0002000000000003e805000000000024201708301256379000'],
    [CardEnum.ALL, '00B202C417', '0001000000000005dc05300040000570201708301223179000'],
    [CardEnum.ALL, '00B203C417', '000100000000004e2001000000000020201708301219179000'],

    [CardEnum.UnionPay3, '00A404000E325041592E5359532E444446303100', '6F36840E325041592E5359532E4444463031A524BF0C21611F4F08A000000333010103501050424F432051554153494352454449548701019000'],
    [CardEnum.UnionPay3, '00A4040008A00000033301010300', '6F498408A000000333010103A53D501050424F432051554153494352454449548701019F38189F66049F02069F03069F1A0295055F2A029A039C019F37045F2D027A68BF0C059F4D020B0A9000'],
    [CardEnum.UnionPay3, '80A8000023832126000000000000000001000000000000015600000000000156200331001122334400', '77818182027C009F3602000357136259071138766890D24012010000029700000F9F1013070A0103A01000010A010000000000C270FA1C9F26089249132C6828543B9F63103031303433333230000000000000A0005F3401009F6C0220009F50060000000000005F201A20202020202020202020202020202020202020202020202020209000'],

    [CardEnum.VISA, '00A404000E325041592E5359532E444446303100', '6F30840E325041592E5359532E4444463031A51EBF0C1B61194F07A0000000031010500B56495341204352454449548701019000'],
    [CardEnum.VISA, '00A4040007A000000003101000', '6F488407A0000000031010A53D500B56495341204352454449548701019F38189F66049F02069F03069F1A0295055F2A029A039C019F37045F2D04656E7A68BF0C089F5A0540015601569000'],
    [CardEnum.VISA, '80A8000023832126000000000000000001000000000000015600000000000156200331001122334400', '77478202200057134693801184579311D24072010000064700000F5F3401019F100706100A03A000009F2608C303C0EBCC51F73D9F360200099F2701809F6C0228009F6E04207000009000'],

    [CardEnum.MC | CardEnum.MC2, '00A404000E325041592E5359532E444446303100', '6F2F840E325041592E5359532E4444463031A51DBF0C1A61184F07A0000000041010500A4D4153544552434152448701019000'],
    [CardEnum.MC | CardEnum.MC2, '00A4040007A000000004101000', '6F338407A0000000041010A528500A4D4153544552434152448701015F2D04656E7A68BF0C0F9F6E07015600003030009F4D020B0A9000'],
    [CardEnum.MC | CardEnum.MC2, '80A8000002830000', '770E82021980940810010201200101009000'],
    [CardEnum.MC, '00B2011400', '7081AB57134455881188579911D23092010000008600000F5A0844558811885799115F24032309305F25031809175F280201565F300202015F3401018C279F02069F03069F1A0295055F2A029A039C019F37049F35019F45029F4C089F34039F21039F7C148D0C910A8A0295059F37049F4C088E0E000000000000000042031E031F039F0702FF009F080200029F0D05B4508400009F0E0500000000009F0F05B4708480009F420201569F4A01829000'],
    [CardEnum.MC2, '00B2011400', '7081879F6C0200019F62060000000700009F630600000078F0009F640104563442313233343536373838383838383030305E202F5E323330323230313030303030303030303030303030303030303030303030309F650200E09F66020F1E9F6701049F6B131234567888888000D23022010000000000000F9F690F9F6A049F7E019F02065F2A029F1A029000'],

    [CardEnum.ALL, '80CA9F4F00', '9F4F199A039F21039F02069F03069F1A025F2A029F4E149C019F36029000'],
    [CardEnum.ALL, '00B2015C00', '18112513200700000000500000000000000001560156B1B1BEA9B4F3D1A7BFDAC7BBD2BDD1A7D4BA000000000A9000'],
    [CardEnum.ALL, '00B2025C00', '18101316101400000066000000000000000001560156B1B1BEA9B4F3D1A7BFDAC7BBD2BDD1A7D4BA00000000099000'],
    [CardEnum.ALL, '00B2035C00', '18091413251600000000500000000000000001560156B1B1BEA9B4F3D1A7BFDAC7BBD2BDD1A7D4BA00000000089000'],
    [CardEnum.ALL, '00B2045C00', '6A83'],

    [CardEnum.ALL, '80CA9F3600', '9F360200169000'],
    [CardEnum.ALL, '80CA9F1700', '9F1701019000'],

    [CardEnum.Octo, '060080080100', '140151525354555657580120220427674EFF8008'], // polling
    [CardEnum.Octo, '10065152535455565758011701018000', '1D075152535455565758000001000003E500000000000000000000000D'], // read balance, addr = 0
    [CardEnum.Octo, '10065152535455565758011701018001', '0C07515253545556575801A8'], // read balance, addr = 1

    [CardEnum.VISA_UP, '00A404000E325041592E5359532E444446303100', '6F50840E325041592E5359532E4444463031A53EBF0C3B61194F07A0000000031010500B5649534120435245444954870101611E4F08A000000333010102500F556E696F6E506179204372656469748701029000'],
    [CardEnum.VISA_UP, '00A4040007A000000003101000', '6F488407A0000000031010A53D500B56495341204352454449548701019F38189F66049F02069F03069F1A0295055F2A029A039C019F37045F2D04656E7A68BF0C089F5A0540015601569000'],
    [CardEnum.VISA_UP, '80A8000023832126000000000000000001000000000000015600000000000156200331001122334400', '774C8202202057134096001333640000D21042010000083200000F5F2002202F5F3401019F100706561203A000009F260822DE38B353390B7E9F360200049F2701809F6C0228009F6E04207000009000'],
    [CardEnum.VISA_UP, '80CA9F1700', '6A88'],
    [CardEnum.VISA_UP, '80CA9F4F00', '6A81'],

];
//let MockCard = CardEnum.Octo;
let MockCard = CardEnum.TU;

function poll() {
    const getType = () => {
        switch (MockCard) {
            case CardEnum.Octo: return "felica";
            case CardEnum.ChinaID: return "unknown";
            default: return "iso7816";
        }
    }
    const getStandard = () => {
        switch (MockCard) {
            case CardEnum.Octo: return "ISO 18092";
            case CardEnum.THU: return "ISO 14443-4 (Type B)";
            case CardEnum.ChinaID: return "ISO 14443-3 (Type B)";
            default: return "ISO 14443-4 (Type A)";
        }
    }
    return Promise.resolve({
        "type": getType(),
        "id": "deadbeef",
        "standard": getStandard(),
        "atqa": '',
        "sak": '',
        "historicalBytes": '',
        "protocolInfo": '',
        "applicationData": '',
        "hiLayerResponse": '',
        "manufacturer": '',
        "systemCode": '',
        "dsfId": ''
    });
}

function transceive(rapdu) {
    rapdu = rapdu.toUpperCase();
    console.log('>', rapdu);
    for (const item of PresetAPDU) {
        if (item[0] & MockCard) {
            if (rapdu == item[1].toUpperCase()) {
                console.log('<', item[2]);
                return Promise.resolve(item[2]);
            }
        }
    }
    // console.log("file not found");
    console.log('<', '6A82');
    return Promise.resolve('6A82');
}

function report(data) {
    console.log('report', data);
}

function finish(data) {
    console.log('finish', data);
}

function log(data) {
    console.log(data);
}
